﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using Microsoft.Xna.Framework.Media;
using System.Collections.Generic;
using System.IO;
using Microsoft.Devices;
using System.Collections.ObjectModel;
using System.Collections;
using System.Linq;
using System.Windows.Media.Imaging;

namespace WindowsPhoneDataBoundApplication1.ViewModels
{
    public class PictureAlbumViewModel : INotifyPropertyChanged
    {
        public PictureAlbumViewModel()
        {
           
        }      

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChange(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
       
        #endregion

        private string name;
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                NotifyPropertyChange("Name");
            }
        }

        private int count;
        public int Count
        {
            get { return count; }
            set
            {
                count = value;
                NotifyPropertyChange("Count");
            }
        }
        /// <summary>
        /// Images Dependency Property
        /// </summary>
        //public static readonly DependencyProperty ImagesProperty =
        //    DependencyProperty.Register("Images", typeof(ObservableCollection<ImageViewModel>), typeof(Album),
        //        new PropertyMetadata((ObservableCollection<ImageViewModel>)null));

        ///// <summary>
        ///// Gets or sets the Images property. This dependency property 
        ///// indicates what are the images.
        ///// </summary>
        //public ObservableCollection<ImageViewModel> Images
        //{
        //    get { return (ObservableCollection<ImageViewModel>)GetValue(ImagesProperty); }
        //    set { SetValue(ImagesProperty, value); }
        //}
        ObservableCollection<ImageViewModel> images = new ObservableCollection<ImageViewModel>();
        public ObservableCollection<ImageViewModel> Images
        {
            get
            {
                if (!Initialized) Initialize();
                return images;
            }
            private set { images = value; }
        }

        bool Initialized { get; set; }
        void Initialize()
        {
                MediaLibrary library = new MediaLibrary();
                var album = library.RootPictureAlbum.Albums.Single(a => a.Name == Name);
                foreach (Picture p in album.Pictures)
                {                    
                    BitmapImage img = new BitmapImage();
                    img.SetSource(p.GetThumbnail());                    
                    images.Add(new ImageViewModel() { Name = p.Name, CreationTime = p.Date, ImageSource = img  });
                }
                Initialized = true;
 
        }
    }
}
